package com.cy.jxc.config;

import at.pollux.thymeleaf.shiro.dialect.ShiroDialect;

import com.cy.jxc.mycache.ShiroCache;
import com.cy.jxc.realm.MyRealm;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.codec.Base64;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.session.mgt.SessionManager;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.CookieRememberMeManager;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.apache.shiro.web.servlet.SimpleCookie;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.ehcache.EhCacheCache;
import org.springframework.cache.ehcache.EhCacheCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    @Bean
    public  ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager){
        ShiroFilterFactoryBean shiroFilterFactoryBean=new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);
        Map<String,String> map=new HashMap<>();
//允许匿名访问的资源
//        map.put("/client/doFindClientByPage", "anon");

//        map.put("/client","anon");
        map.put("/register", "anon");
//        map.put("/login", "anon");
        map.put("/static/**","anon");
        map.put("/error/**", "anon");
        map.put("/login/loginbyname", "anon");
        map.put("/login/login","anon");
//        map.put("/goods", "anon");
        map.put("/goods/goods", "anon");
        map.put("/index_jumbotron", "anon");
        map.put("/register/**","anon");
        map.put("/login/logout", "logout");
//        map.put("/login/**", "authc");
        map.put("/index", "user");
        map.put("/**","user");//authc
        shiroFilterFactoryBean.setLoginUrl("/login");
        shiroFilterFactoryBean.setSuccessUrl("/index");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        return shiroFilterFactoryBean;

    }
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(CacheManager cacheManager,@Qualifier("realm") Realm realm,CookieRememberMeManager cookieRememberMeManager){
        DefaultWebSecurityManager defaultWebSecurityManager=new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        defaultWebSecurityManager.setRememberMeManager(cookieRememberMeManager);
        defaultWebSecurityManager.setSessionManager(sessionManager());
        return defaultWebSecurityManager;
    }
    @Bean("realm")
    public Realm getRealm(CacheManager cacheManager){
        MyRealm myRealm = new MyRealm();
        myRealm.setCacheManager(cacheManager);
//        开启全文缓存
        myRealm.setCachingEnabled(true);

        //开启认证缓存
        myRealm.setAuthenticationCachingEnabled(true);
        myRealm.setAuthenticationCacheName("AuthenticationCache");
//        开启授权缓存
        myRealm.setAuthorizationCachingEnabled(true);
        myRealm.setAuthorizationCacheName("AuthorizationCache");
        return myRealm;
    }
    @Bean
    public SimpleCookie getSimpleCookie(){
        SimpleCookie simpleCookies=new SimpleCookie("remember");
        simpleCookies.setMaxAge(60*60*24*7);
        return simpleCookies;
    }
    @Bean
    public CookieRememberMeManager getCookieRememberMeManager(SimpleCookie simpleCookies){
        CookieRememberMeManager cookieRememberMeManager=new CookieRememberMeManager();
        cookieRememberMeManager.setCookie(simpleCookies);
        cookieRememberMeManager.setCipherKey(Base64.decode("2AvVhdsgUs0FSA3SDFAdag=="));
        return cookieRememberMeManager;
    }
//配置缓存整合redis
    @Bean
    public CacheManager getCacheManager(){
        return  new ShiroCache();
    }
//    配置session
    @Bean
    public SessionManager sessionManager() {
        DefaultWebSessionManager sManager=
                new DefaultWebSessionManager();
        sManager.setGlobalSessionTimeout(60*60*1000);
        sManager.setSessionIdUrlRewritingEnabled(false);
        return sManager;
    }

}
